home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_12 / allison / rtti.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-04  |  762 b   |  38 lines

  1. LISTING 5 - Illustrates RTTI features
  2.  
  3. #include <iostream.h>
  4. #include <typeinfo.h>
  5.  
  6. main()
  7. {
  8.     B b;
  9.     D d;
  10.     B *bp = &d;
  11.  
  12.     if (dynamic_cast<D*>(bp))
  13.         cout << "downcast OK\n";
  14.     else
  15.         cout << "downcast not OK\n";
  16.  
  17.     cout << "b's type name == "
  18.          << typeid(b).name() << endl;
  19.     cout << "d's type name == "
  20.          << typeid(d).name() << endl;
  21.     cout << "bp's type name == "
  22.          << typeid(bp).name() << endl;
  23.     cout << "b and d are "
  24.          << (typeid(b) == typeid(d)
  25.              ? ""
  26.              : "not ")
  27.          << "the same type" << endl;
  28.     return 0;
  29. }
  30.  
  31. /* Output:
  32. downcast OK
  33. b's type name == B
  34. d's type name == D
  35. bp's type name == B *
  36. b and d are not the same type
  37. */
  38.